#include<iostream>
#include<vector>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
template<class T>
class RuningQueue
{
   private:
     std::vector<T> v;
     sem_t data_sem;//数据
     sem_t block_sem;//空间
     int c_index;
     int p_index;
     int cap;
   public:
     void P(sem_t &s)
     {
        sem_wait(&s);
     }

     void V(sem_t &s)
     {
        sem_post(&s);
     }
   public:     
     RuningQueue(int _cap = 6)
       :cap(_cap)
        ,p_index(0)
        ,c_index(0)
  {
     sem_init(&data_sem,0,0);
     sem_init(&block_sem,0,cap);
     v.resize(10);

  }
     void Put(const T& data)
     {
        P(block_sem);//生产者,
        v[p_index] = data;
        p_index++;
        p_index %= cap;
        V(data_sem);
     }
     void Get(T& data)
     {

        P(data_sem);//生产者,
        data = v[c_index];
        c_index++;
        c_index %= cap;
        V(block_sem);
     }
    ~RuningQueue()
    {
      sem_destroy(&data_sem);     
      sem_destroy(&block_sem);     
    }
};
